from rest_framework import serializers
from apps.backend import models

class LoginSerializer(serializers.ModelSerializer):
    captcha = serializers.CharField()

    class Meta:
        model = models.SystemAdmin
        fields = ['username', 'password', 'captcha'] 
        extra_kwargs = {
            "username": {
                "max_length": 20,
                "min_length": 5
            },
            "password": {
                "max_length": 20,
                "min_length": 6, 
            }
        }

    def validate_captcha(self, value):
        if len(value) != 4:
            raise serializers.ValidationError("验证码格式错误")
        
        return value